import { Modal, message } from 'antd';
import { ExclamationCircleFilled } from '@ant-design/icons'
import type { ActionType } from '@ant-design/pro-table';
import { STATUS_CODE } from '@/common/const'
const { confirm } = Modal;

type ConfirmProps = {
    id: string,
    action?: ActionType,
    deleteApi: (id) => Promise<any>,
    cb?: () => void
}

const showDeleteConfirm: (props: ConfirmProps) => void = ({ id, action, deleteApi, cb }) => {
    confirm({
        title: '确认删除?',
        width: 450,
        icon: <ExclamationCircleFilled />,
        content: '删除后内容无法恢复，请确认是否要执行删除操作？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk: () => {
            return new Promise(async (resolve, reject) => {
                const { code } = await deleteApi(id)
                if (code === STATUS_CODE.SUCCESS) {
                    message.success('删除成功！')
                    action?.reload(true);
                    cb?.()
                } else {
                    message.error('删除失败！')
                }
                resolve(true);
            })
        }
    });
};

export default showDeleteConfirm;